package com.qtyx.nms.custom;

import android.content.Context;
import android.view.animation.Interpolator;
import android.widget.Scroller;

import androidx.viewpager.widget.ViewPager;

import java.lang.reflect.Field;

/**
 * ViewPager 滚动速度设置
 */
public class ViewPagerScrollerSpeed extends Scroller {
    private int mScrollDuration = 1000; // 滑动速度

    public ViewPagerScrollerSpeed(Context context) {
        super(context);
    }
    public ViewPagerScrollerSpeed(Context context, Interpolator interpolator) {
        super(context, interpolator);
    }

    public ViewPagerScrollerSpeed(Context context, Interpolator interpolator, boolean flywheel) {
        super(context, interpolator, flywheel);
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
        super.startScroll(startX, startY, dx, dy, mScrollDuration);
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy) {
        super.startScroll(startX, startY, dx, dy, mScrollDuration);
    }

    /**
     * 设置滑动时间
     */
    public void setScrollSpeed(ViewPager viewPager, int mScrollDuration) {
        this.mScrollDuration=mScrollDuration;
        try {
            Field mScroller = ViewPager.class.getDeclaredField("mScroller");
            mScroller.setAccessible(true);
            mScroller.set(viewPager, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


